﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.YouTube;
using Google.YouTube;

namespace YoutubeVideoManager
{
    /// <summary>
    /// Logica di interazione per PlaylistView.xaml
    /// </summary>
    public partial class PlaylistView : UserControl
    {
        #region Event
        public event EventHandler DownloadCompleted;
        public event SelectionChangedEventHandler SelectionChanged;
        public event EventHandler PlaylistDoubleClick;
        #endregion

        #region Properties
        #endregion

        #region Fields
        BackgroundWorker worker;
        int retrieved, downloaded;
        #endregion

        #region Methods
        public PlaylistView()
        {
            InitializeComponent();

            worker = new BackgroundWorker();
            worker.WorkerSupportsCancellation = worker.WorkerReportsProgress = true;
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
        }

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = true;
            try
            {
                retrieved = downloaded = 0;
                string user = e.Argument as string;
                Feed<Playlist> userPlaylists = Global.Request.GetPlaylistsFeed(user);
                foreach (Playlist entry in userPlaylists.Entries)
                {
                    retrieved++;
                    if (worker.CancellationPending)
                        return;
                    worker.ReportProgress(0, entry);
                }
            }
            catch { e.Result = false; }
        }

        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            PlaylistItem pi = new PlaylistItem(e.UserState as Playlist);
            pi.MouseDoubleClick += new MouseButtonEventHandler(pi_MouseDoubleClick);
            pi.DownloadCompleted += new EventHandler(pi_DownloadCompleted);
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (!(bool)e.Result || retrieved == 0)
            {
                WorkCompleted();
            }
        }

        void pi_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (PlaylistDoubleClick != null)
                PlaylistDoubleClick(sender, EventArgs.Empty);
        }

        void pi_DownloadCompleted(object sender, EventArgs e)
        {
            this.lvPlaylists.Items.Add(sender);
            if (++downloaded == retrieved)
            {
                WorkCompleted();
            }
        }

        void WorkCompleted()
        {
            this.prgBar.Visibility = System.Windows.Visibility.Collapsed;
            if (DownloadCompleted != null)
                DownloadCompleted(this, null);
        }

        /// <summary>
        /// Carica le playlist dell'utente specificato
        /// </summary>
        /// <param name="username"></param>
        public void SetUser(string username)
        {
            while (worker.IsBusy)
                worker.CancelAsync();

            lvPlaylists.Items.Clear();
            prgBar.Visibility = System.Windows.Visibility.Visible;
            worker.RunWorkerAsync(username);
        }

        private void lvPlaylists_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SelectionChanged != null)
            {
                SelectionChanged(this, e);
            }
        }
        #endregion


    }
}
